import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决Vue Router 3.x版本中，频繁点击相同路由导致的控制台报错问题
// 重写push方法，捕获异常并不做处理（避免错误提示）
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 注册路由插件
Vue.use(VueRouter)

// 路由配置数组
const routes = [
  {
    path: '/', // 根路径
    name: 'Manager', // 路由名称
    component: () => import('../views/Manager.vue'), // 懒加载管理员布局组件
    redirect: '/home',  // 访问根路径时自动重定向到首页
    children: [ // 嵌套路由（管理员后台页面）
      { path: '403', name: 'Auth', meta: { name: '无权限' }, component: () => import('../views/manager/Auth.vue') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home.vue') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin.vue') },
      { path: 'user', name: 'User', meta: { name: '用户信息' }, component: () => import('../views/manager/User.vue') },
      { path: 'person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/manager/Person.vue') },
      { path: 'notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice.vue') },
      { path: 'echarts', name: 'Echarts', meta: { name: '数据统计' }, component: () => import('../views/manager/Echarts.vue') },
      { path: 'category', name: 'Category', meta: { name: '分类信息' }, component: () => import('../views/manager/Category.vue') },
      { path: 'book', name: 'Book', meta: { name: '书籍信息' }, component: () => import('../views/manager/Book.vue') },
      { path: 'comment', name: 'Comment', meta: { name: '评论信息' }, component: () => import('../views/manager/Comment.vue') },
      { path: 'collect', name: 'Collect', meta: { name: '收藏信息' }, component: () => import('../views/manager/Collect.vue') },
      { path: 'carousel', name: 'Carousel', meta: { name: '轮播图信息' }, component: () => import('../views/manager/Carousel.vue') },
      { path: 'borrow', name: 'Borrow', meta: { name: '借阅信息' }, component: () => import('../views/manager/Borrow.vue') },
      { path: 'echarts', name: 'Echarts', meta: { name: '数据统计' }, component: () => import('../views/manager/Echarts.vue') },
    ]
  },
  {
    path: '/front', // 前端用户页面根路径
    name: 'Front', // 路由名称
    component: () => import('../views/front/Front.vue'), // 懒加载前端布局组件
    redirect: '/front/home',  // 访问/front时重定向到前端首页
    children: [ // 嵌套路由（前端用户页面）
      { path: 'home', name: 'Home', meta: { name: '首页信息' }, component: () => import('../views/front/Home.vue') },
      { path: 'person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/front/Person.vue') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/front/Password.vue') },
      { path: 'bookDetail', name: 'BookDetail', meta: { name: '详情' }, component: () => import('../views/front/BookDetail.vue') },
      { path: 'category', name: 'Category', meta: { name: '分类' }, component: () => import('../views/front/Category.vue') },
      { path: 'borrow', name: 'Borrow', meta: { name: '我的借阅' }, component: () => import('../views/front/Borrow.vue') },
      { path: 'collect', name: 'Collect', meta: { name: '我的收藏' }, component: () => import('../views/front/Collect.vue') },
      { path: 'notice', name: 'Notice', meta: { name: '公告详情' }, component: () => import('../views/front/Notice.vue') },
      { path: 'noticeDetail', name: 'NoticeDetail', meta: { name: '公告列表' }, component: () => import('../views/front/NoticeDetail.vue') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
  { path: '*', name: '404', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

// 创建路由实例
const router = new VueRouter({
  mode: 'history', // 路由模式：history模式（无#号），需后端配合配置
  base: process.env.BASE_URL, // 应用的基路径，从环境变量获取
  routes // 路由配置
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('user') || '{}')
  if (to.path === '/'){
    if (user.role){
      if (user.role === 'ADMIN'){
        next('/home')
      } else {
        next('/front/home')
      }
    } else {
      next('/login')
    }
  } else {
    if (to.matched.length === 0){
      next('/404')
      return
    } else {
      next()
    }
  }
})
// 导出路由实例
export default router
